$(function() {
	//获取服务器当前时间
	var http = new XMLHttpRequest;
	http.open("HEAD", ".", false);
	http.send(null);
	var today = new Date(http.getResponseHeader("Date"));
	var year_today = today.getFullYear();
	var month_today = (today.getMonth() + 1) < 10 ? "0" + (today.getMonth() + 1) : (today.getMonth() + 1);
	var lastMonth = "";
	if(month_today == "01") {
		lastMonth = "12";
	}else{
		lastMonth = today.getMonth() < 10 ? ("0" + today.getMonth()) : today.getMonth();
	}
	//desktop.js传递的时间
	var year = $("#getYear").val();
	var month =  $("#getMonth").val();
	var timeStr = year + "-" + month;

	//返回按钮
	$("#goBack").click(function() {
		history.back();
	});

	var init =function(timeStr) {
		if(timeStr == "-" ){
			if(month_today == "01") {
				timeStr = (year_today - 1) + "-12";
			}else{
				timeStr = year_today + "-" + lastMonth;
			}
			
		}
		
		
		//去除前后空格
		timeStr = timeStr.replace(/(^\s*)|(\s*$)/g, "");
		
		$.ajax({
			url: '/api/performance/selMonthPerf/' + timeStr + '?',
			type: 'get',
			data: {'_t': new Date().getTime()},
			success: function(data) {
				// console.info(data);
				if(data != null && data.perData != null && data.perData.length != 0 && data.perData[0].state == 2){
					$("#tip2").html("");
					$("#perfMainForm").css("display","block");
					var perfData = data.perData[0];
				    var termsData = data.termsData;
			 	    $("#perfDate").val(perfData.perfDate);
			 	    $("#employee").val(perfData.empName);
			 	    // $("#assessmentPerson").val(perfData.assname);
			 	    $("#perfScore").html(perfData.score);
			 	    $("#perfLevel").html(perfData.level);
			 	    $("#comment").html(perfData.comment);
			 	    getOrgParents(perfData.departmentId, 0);
			 	    if(perfData.preDeptId != null) {
			 	    	getOrgParents(perfData.preDeptId, 1);
			 	    }
			 	    if(perfData.project != null) {
			 	    	$("#project").val(perfData.project);
			 	    }
			 	    showTerms(termsData);
				}else{
					$("#perfMainForm").css("display","none");
    		        $('#tip2').html("<div class='alert alert-warning' style='margin-top:-20px;'>未能搜索到上个月绩效相关信息</div>");
					// return;
				}
				
			}
		});

		showHisPerfs(1);

	};

	//查看当前登录用户的历史绩效成绩
	var showHisPerfs = function(cpage) {
		var item = 10;
		// var timeStr = year_today + "-" + month_today;

		$.ajax({
			url: '/api/performance/selHisPerfs?',
			type: 'get',
			data: {'index': cpage, 'item':item, '_t':new Date().getTime()},
			success: function(results) {
				// console.log(results);
				// $("#showCount").html("共"+ results.count + "条记录");
				var page;
				if (results.count < item) {
					page = 1;
				} else {
					page = Math.ceil(results.count / item);
				}

				var options = {
					currentPage: cpage,
					totalPages: page,
					alignment: 'right',
					count:results.count,
					selectorId: 'pageSeletct',
					onPageChanged: function(e, oldPage, newPage) {
						showHisPerfs(newPage);
					}
				};

				$('#paginator').bootstrapPaginator(options);
				$("#pageSeletct").change(function() {
					var page = $("#pageSeletct option:selected").val();
					$('#paginator').bootstrapPaginator("show",page);
				});
				showList(results.data);
			}
		});
	}

	init(timeStr);
	var showTerms = function(termsData) {
		if(termsData != null && termsData.length != 0) {
			$("#tbd_task").html('');
			$("#tbd_pluse").html('');
			$("#tbd_professional").html('');
			var plusFlag = false;
			$.each(termsData, function(i, item) {
				var templateData = {
					    term : item.term,
					    content : item.content,
					    weight : item.weight+"%",
					    completion: item.completion,
					    score: item.score,
					    name:item.name
				    };
				var template;
				if(item.type == 0) {
					//工作事项

				    template = "<tr><td style='text-align:center;width:20%;word-wrap:break-word;vertical-align:middle;'>{{term}}</td>"+
				                   "<td style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;'>{{content}}</td>"+
				                   "<td class='hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;'>{{weight}}</td>"+
				                   "<td class='hidden-phone' style='text-align:center;width:20%;word-wrap:break-word;vertical-align:middle;'>{{completion}}</td>"+
				                   "<td class='hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;'>{{score}}</td>"+
				                   "<td class='hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;'>{{name}}</td></tr>";
				    var $html = $(Mustache.to_html(template, templateData));
				    $("#tbd_task").append($html);

				}else if(item.type == 1) {
					plusFlag = true;
					//加分项
					template = "<tr><td style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;'>{{term}}</td>"+
							   "<td style='text-align:center;width:40%;word-wrap:break-word;vertical-align:middle;'>{{content}}</td>"+
							   "<td class='hidden-480' style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;'>{{score}}</td></tr>";

                    var $html = $(Mustache.to_html(template, templateData));
				    $("#tbd_pluse").append($html);
				}else{
					//内容及标准
					template = "<tr><td style='text-align:center;width:30%;word-wrap:break-word;vertical-align:middle;'>{{term}}</td>"+					
							   "<td class='hidden-480' style='text-align:center;width:10%;word-wrap:break-word;vertical-align:middle;'>{{weight}}</td>"+
							   "<td class='hidden-phone' style='text-align:center;width:40%;word-wrap:break-word;vertical-align:middle;'>{{completion}}</td>"+
							   "<td class='hidden-480' style='text-align:center;width:20%;word-wrap:break-word;vertical-align:middle;'>{{score}}</td></tr>";
                    
                    var $html = $(Mustache.to_html(template, templateData));
				    $("#tbd_professional").append($html);
				}
			});
            if(plusFlag) {
            	//显示加分项
            	$("#showPlus").css("display", "block");
            }
		}else{
			return;
		}
	};
    

    var showList = function(data) {
    	$("#tbd_perf").html("");
    	
    	if(data == null || data.length == 0) {
    		$('#tip').html("<div class='alert alert-warning' style='margin-top:-20px;'>未能搜索到相关信息</div>");
    	}else{
    		$("#tip").html("");
    	}
    	$.each(data, function(i, item) {
    		var showState = item.score + "(" + item.level + ")";
    		
    		var templateData = {
    			perfId : item.id,
    			perfDate: item.perfDate,
    			showState: showState
    		};
    		var template = "<tr><td class='center' id='perfDate'>{{perfDate}}</td>"+
    		        "<input type='hidden' id='perfId' name='perfId' value={{perfId}} />"+
    				"<td class='center'>{{showState}}</td>"+
    				"<td class=center id=showPerfDetail style='cursor:pointer;'><i class='icon-edit bigger-150'></i>&nbsp;查看详细</td></tr>";

    		var $html = $(Mustache.to_html(template, templateData));
    		$("#tbd_perf").append($html);

    		$html.find('#showPerfDetail').click({
    			html: $html
    		}, function(e) {
    			var $html = $(e.data.html);
    			var perfDate = $html.find("#perfDate").html();
    			// alert(perfDate);
    			init(perfDate);
    			$("#perfDetail").tab("show");
    		});
    	});

    };
	

	var getOrgParents = function(orgId, type) {
	$.ajax({
		url: '/api/organizations/getParents/' + orgId + '?',
		type: 'get',
		data: {'_t':new Date().getTime()},
		success: function(result) {		          
			// console.info("lijuanxia");
			// console.info(result);
			var parentString = "";
			for(var i = result.length-1; i >= 0; i--){
				if(i != 0) {
					if(result[i].orgName == "分公司") {
						continue;
					}else{
						parentString += result[i].orgName + "/";
					}
				}else{
					parentString += result[i].orgName;
				}
			}
			if(type == 1) {
				$("#projectDept").val(parentString);
			}else{
				$("#department").val(parentString);
			}
			
			// $("#deptId").val(result[0].id);

		}
	});
};
	
});